package com.quartet.modules.vehicle.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.quartet.api.ApiResult;
import com.quartet.api.PageResult;
import com.quartet.modules.vehicle.dto.*;
import com.quartet.modules.vehicle.entity.TVehicleInfo;
import com.quartet.modules.vehicle.service.TVehicleInfoService;
import com.quartet.utils.BeanTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆摘要信息表(TVehicleInfo)表控制层
 *
 * @author makejava
 * @since 2021-05-04 17:26:14
 */
@RestController
@RequestMapping("tVehicleInfo")
@Api(tags = "业务：车辆摘要信息")
@Slf4j
public class TVehicleInfoController  {
    /**
     * 服务对象
     */
    @Resource
    private TVehicleInfoService tVehicleInfoService;

    /**
     * 分页查询所有车辆数据
     *
     * @param tVehicleInfoRequestDto 查询实体
     * @return 所有数据
     */
    @ApiOperation("查询所有车辆报警数据")
    @PostMapping("/selectVehicleByCondition")
    public ApiResult<PageResult<TVehicleInfoResponseDto>> selectVehicleByCondition(@RequestBody TVehicleInfoRequestDto tVehicleInfoRequestDto) {
        log.info("---------selectVehicleByCondition begin, tVehicleInfoRequestDto is {}----------", tVehicleInfoRequestDto);
        PageResult<TVehicleInfoResponseDto> pageResult = tVehicleInfoService.selectVehicleByCondition(tVehicleInfoRequestDto);
        return ApiResult.ok(pageResult);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectVehicleInfoById")
    public ApiResult<TVehicleInfoTotalResponseDto> selectVehicleInfoById(@RequestParam("id") Integer id) {
        TVehicleInfoTotalResponseDto tVehicleInfoTotalResponseDto = tVehicleInfoService.selectVehicleInfoById(id);
        return ApiResult.ok(tVehicleInfoTotalResponseDto);
    }

    /**
     * 新增数据
     *
     * @param tVehicleInfoTotalRequestDto 实体对象
     * @return 新增结果
     */
//    @PostMapping("/addVehicleInfo")
    @ApiOperation("添加车辆相关信息")
    @RequestMapping(value = "/addVehicleInfo", method = RequestMethod.POST)
    public ApiResult addVehicleInfo(@RequestParam("driverPhotoList") List<MultipartFile> driverPhotoList, TVehicleInfoTotalRequestDto tVehicleInfoTotalRequestDto) {
        log.info("---------addVehicleInfo begin, tVehicleInfoTotalRequestDto is {}----------", tVehicleInfoTotalRequestDto);
        this.tVehicleInfoService.addVehicleInfo(tVehicleInfoTotalRequestDto, driverPhotoList);
        return ApiResult.ok();
    }

    /**
     * 修改数据
     *
     * @param tVehicleInfoTotalRequestDto 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改车辆相关信息")
    @PutMapping("/updateVehicleInfo")
    public ApiResult updateVehicleInfo(@RequestBody TVehicleInfoTotalRequestDto tVehicleInfoTotalRequestDto) {
        log.info("---------updateVehicleInfo begin, tVehicleInfoTotalRequestDto is {}----------", tVehicleInfoTotalRequestDto);
        this.tVehicleInfoService.updateVehicleInfo(tVehicleInfoTotalRequestDto);
        return ApiResult.ok();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/deleteById")
    public ApiResult deleteById(@RequestParam("id") Integer id) {
        tVehicleInfoService.deleteById(id);
        return ApiResult.ok();
    }

    /**
     * 根据客户编码查询车辆信息集合
     *
     * @param customerCode 客户编码
     * @return
     */
    @PostMapping("/selectVehicleByCustomerCode")
    public ApiResult<TVehicleInfoAddRequestDto> selectVehicleByCustomerCode(@RequestParam("customerCode") String customerCode) {
        TVehicleInfo tVehicleInfo = tVehicleInfoService.getOne(new QueryWrapper<TVehicleInfo>().lambda().eq(TVehicleInfo::getCustomerCode, customerCode));
        TVehicleInfoAddRequestDto tVehicleInfoAddRequestDto = BeanUtil.copyProperties(tVehicleInfo, TVehicleInfoAddRequestDto.class);
        return ApiResult.ok(tVehicleInfoAddRequestDto);
    }

    /**
     * 查询全部车辆信息集合
     *
     * @return
     */
    @PostMapping("/selectVehicleList")
    public ApiResult<List<TVehicleInfoAddRequestDto>> selectVehicleList() {
        List<TVehicleInfo> tVehicleInfoList = tVehicleInfoService.list();
        List<TVehicleInfoAddRequestDto> tVehicleInfoAddRequestDtoList = BeanTool.copyObjListProperties(tVehicleInfoList, TVehicleInfoAddRequestDto.class);
        return ApiResult.ok(tVehicleInfoAddRequestDtoList);
    }
}
